package com.quickd.service.service.wms.impl;

import cn.hutool.core.bean.BeanUtil;
import com.quickd.core.secure.SecureUtil;
import com.quickd.core.service.impl.BaseServiceImpl;
import com.quickd.service.entity.wms.Flow;
import com.quickd.service.entity.wms.Form;
import com.quickd.service.mapper.wms.FormDetailMapper;
import com.quickd.service.entity.wms.FormDetail;
import com.quickd.service.service.wms.IFlowService;
import com.quickd.service.service.wms.IFormDetailService;
import com.quickd.service.service.wms.IFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 单据明细
 *
 * @author quickd quickd@dacanginfo.com
 * @since 2019-10-08
 */
@Service
public class FormDetailServiceImpl extends BaseServiceImpl<FormDetailMapper, FormDetail> implements IFormDetailService {
    @Autowired
    private IFlowService iFlowService;

    @Autowired
    private IFormService iFormService;

    @Override
    @Transactional(rollbackFor =Exception.class ,propagation= Propagation.REQUIRED)
    public void addForm(Form form, List<FormDetail> formDetails) {
        for(FormDetail formDetail:formDetails) {
            form.setWorker(SecureUtil.getCurrentUser().getUsername());
            form.setOperator(SecureUtil.getCurrentUser().getUsername());
            iFormService.save(form);
            Double quantity = 0.0;
            quantity += formDetail.getQuantity();
            formDetail.setFormId(form.getId());
            baseDao.insert(formDetail);
            Flow flow = new Flow();
            flow.setFormId(form.getId());
            BeanUtil.copyProperties(form, flow, "id");
            flow.setMaterialCode(formDetail.getMaterialCode());
            flow.setMaterialName(formDetail.getMaterialName());
            flow.setQuantity(quantity);
            iFlowService.save(flow);
        }
    }
}